<template>
  <div>
    <!-- 添加更新学期信息 -->
    <el-dialog
      title="信息详情"
      :visible.sync="isShowSemesterInfo"
      width="40rem"
      :before-close="closeSemesterInfo"
    >
      <el-form :ref="semesterInfo" :model="semesterInfo" class="demo-form-inline" :rules="semesteRules">
        <el-form-item label="学期名称" label-width="5rem" prop="semName">
          <el-input
            v-model="semesterInfo.semName"
            style="width:25rem"
            :placeholder="semesterInfo.semName"
            clearable
            maxlength="20"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="审核时间" label-width="5rem">
          <el-date-picker
            v-model="semCheckTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="提交时间" label-width="5rem">
          <el-date-picker
            v-model="semSendTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="是否设置为当前学期" prop="semStatus">
          <el-radio-group v-model="semesterInfo.semStatus" @change="updateSemStatus()">
            <el-radio :label="1">不设置</el-radio>
            <el-radio v-if="openSemTotal=== 0 || semesterInfo.semStatus === 0" :label="0">设置为当前学期</el-radio>
            <el-tooltip v-if="openSemTotal!== 0 && semesterInfo.semStatus !== 0" class="item" effect="dark" content="当前存在已开放的学期" placement="bottom">
              <el-radio disabled :label="0">设置为当前学期</el-radio>
            </el-tooltip>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeSemesterInfo()">取 消</el-button>
        <el-button type="primary" @click="sendSemesterInfo()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { updateSemester, addSemester } from '../../api/semester.js'
export default {
  data() {
    return {
      // 页面数据存储位置
      semesterInfo: {}, // 学期信息详情
      semCheckTime: [], // 学期信息详情审核时间范围
      semSendTime: [], // 学期信息详情提交时间范围
      openSemTotal: 0, // 学期开放数量
      // 详情数据缓存处理
      semInfoSemName: '',
      semInfoSemStatus: 0,
      semInfoSemCheckStartTime: '',
      semInfoSemCheckStopTime: '',
      semInfoSemSendStartTime: '',
      semInfoSemSendStopTime: '',
      // 代码数据存储位置
      // 表单判断规则
      semesteRules: {
        semName: [
          { required: true, message: '请输入学期名称', trigger: 'change' }
        ],
        semStatus: [
          { required: true, message: '请选择学期状态', trigger: 'change' }
        ]
      },
      isShowSemesterInfo: false,
      sendInfoStatus: 0 // 提交状态：0未设置，1添加，2修改，3删除
    }
  },
  created() {

  },
  methods: {
    // 展示Dialog
    showDialog(semesterInfo, sendInfoStatus, openSemTotal) {
      var that = this
      that.sendInfoStatus = sendInfoStatus
      that.openSemTotal = openSemTotal
      if (sendInfoStatus === 1) {
        if (that.semesterInfo !== {}) {
          that.semesterInfo = {}
          that.semCheckTime = []
          that.semSendTime = {}
        }
        this.isShowSemesterInfo = true
      } else if (sendInfoStatus === 2) {
        if (that.semCheckTime !== []) {
          that.semCheckTime = []
        }
        if (that.semSendTime !== []) {
          that.semSendTime = []
        }
        this.semInfoSemName = semesterInfo.semName
        this.semInfoSemStatus = semesterInfo.semStatus
        this.semInfoSemCheckStartTime = semesterInfo.semCheckStartTime
        this.semInfoSemCheckStopTime = semesterInfo.semCheckStopTime
        this.semInfoSemSendStartTime = semesterInfo.semSendStartTime
        this.semInfoSemSendStopTime = semesterInfo.semSendStopTime
        this.semesterInfo = semesterInfo
        this.semCheckTime[0] = semesterInfo.semCheckStartTime
        this.semCheckTime[1] = semesterInfo.semCheckStopTime
        this.semSendTime[0] = semesterInfo.semSendStartTime
        this.semSendTime[1] = semesterInfo.semSendStopTime
        this.isShowSemesterInfo = true
      }
    },
    // 提交信息
    sendSemesterInfo() {
      var that = this
      // 判断是否填全信息
      this.$refs[that.semesterInfo].validate((valid) => {
        if (valid) {
          // 判断是否填写时间
          if (that.semCheckTime === null || that.semCheckTime[0] === undefined || that.semCheckTime[1] === undefined) {
            this.$message({
              type: 'info',
              message: '请填写审核时间'
            })
          } else if (that.semSendTime === null || that.semSendTime[0] === undefined || that.semSendTime[1] === undefined) {
            this.$message({
              type: 'info',
              message: '请填写提交时间'
            })
          } else {
            var sendSemesterAllInfo = {}
            // 时间转化
            sendSemesterAllInfo.semName = that.semesterInfo.semName
            sendSemesterAllInfo.semStatus = that.semesterInfo.semStatus
            sendSemesterAllInfo.semCheckStartTime = that.dateReSend(that.semCheckTime[0])
            sendSemesterAllInfo.semCheckStopTime = that.dateReSend(that.semCheckTime[1])
            sendSemesterAllInfo.semSendStartTime = that.dateReSend(that.semSendTime[0])
            sendSemesterAllInfo.semSendStopTime = that.dateReSend(that.semSendTime[1])
            // 根据状态提交信息
            if (that.sendInfoStatus === 1) {
            // 添加学期信息
              addSemester(sendSemesterAllInfo).then((res) => {
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.isShowSemesterInfo = false
                setTimeout(() => {
                  location.reload()
                }, 300)
              })
            } else if (that.sendInfoStatus === 2) {
            // 修改学期信息
              sendSemesterAllInfo.semId = that.semesterInfo.semId
              updateSemester(sendSemesterAllInfo).then((res) => {
                this.$message({
                  type: 'success',
                  message: '修改成功'
                })
                if (sendSemesterAllInfo.semStatus === 0) {
                  sessionStorage.setItem('semId', sendSemesterAllInfo.semId)
                  sessionStorage.setItem('semName', sendSemesterAllInfo.semName)
                  sessionStorage.setItem('semCheckStartTime', sendSemesterAllInfo.semCheckStartTime)
                  sessionStorage.setItem('semCheckStopTime', sendSemesterAllInfo.semCheckStopTime)
                } else if (sendSemesterAllInfo.semStatus === 1) {
                  sessionStorage.setItem('semId', '无开放学期')
                  sessionStorage.removeItem('semName')
                  sessionStorage.removeItem('semCheckStartTime')
                  sessionStorage.removeItem('semCheckStopTime')
                }
                this.isShowSemesterInfo = false
                setTimeout(() => {
                  location.reload()
                }, 300)
              })
            }
          }
        } else {
          this.$message({
            type: 'info',
            message: '请补全信息后提交'
          })
          return false
        }
      })
    },
    // 更新学期状态
    updateSemStatus() {
    },
    // 页面逻辑方法
    // 关闭详情
    closeSemesterInfo(done) {
      var that = this
      that.$confirm('确认关闭？')
        .then(_ => {
          that.semesterInfo.semName = that.semInfoSemName
          that.semesterInfo.semStatus = that.semInfoSemStatus
          that.semesterInfo.semCheckStartTime = that.semInfoSemCheckStartTime
          that.semesterInfo.semCheckStopTime = that.semInfoSemCheckStopTime
          that.semesterInfo.semSendStartTime = that.semInfoSemSendStartTime
          that.semesterInfo.semSendStopTime = that.semInfoSemSendStopTime
          that.isShowSemesterInfo = false
        })
        .catch(_ => {})
    },
    // 时间转化
    dateReSend(d) {
      // 判断是否需要转化
      var isDateReSend = d.toString().search('-')
      if (isDateReSend !== -1) {
        return d
      } else {
        var rebackDate = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds()
        return rebackDate
      }
    }
  }
}
</script>
<style lang="scss">

</style>
